package org.gameg.Api.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import springfox.documentation.spring.web.json.Json;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

public class TestNaem {
    public static void main(String[] args) throws IOException {
        String jsonStr = "{\"1\":\"1111\",\"2\":[\n" +
                "        {\n" +
                "            \"point\": \"1\",\n" +
                "            \"isOpen\": \"Y\",\n" +
                "            \"niuPoint\": \"1\",\n" +
                "            \"power\": \"1\",\n" +
                "            \"betSum\": \"1\",\n" +
                "            \"xianWinDrow\": \"1\",\n" +
                "            \"winGdDrow\": \"1\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"point\": \"2\",\n" +
                "            \"isOpen\": \"Y\",\n" +
                "            \"niuPoint\": \"2\",\n" +
                "            \"power\": \"2\",\n" +
                "            \"betSum\": \"1\",\n" +
                "            \"xianWinDrow\": \"1\",\n" +
                "            \"winGdDrow\": \"1\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"point\": \"3\",\n" +
                "            \"isOpen\": \"Y\",\n" +
                "            \"niuPoint\": \"3\",\n" +
                "            \"power\": \"3\",\n" +
                "            \"betSum\": \"1\",\n" +
                "            \"xianWinDrow\": \"1\",\n" +
                "            \"winGdDrow\": \"1\"\n" +
                "        }]}";
        JSONObject jsonObject = JSON.parseObject(jsonStr);
        System.out.println(jsonObject.get("2"));

        Properties pro = new Properties();
        pro.load(new FileInputStream("src/main/resources/property.properties"));
        for (String key :
                jsonObject.keySet()) {
            if (jsonObject.get(key) instanceof JSONArray) {
                System.out.println(true);
                JSONArray jsonArray = (JSONArray) jsonObject.getJSONArray(key);
                for (int i = 0; i < jsonArray.size(); i++) {
                    JSONObject itemObj = (JSONObject) jsonArray.get(i);
                    for (String key2 :
                            itemObj.keySet()) {
                        pro.setProperty(key + "[" + i + "]."+key2, itemObj.getString(key2));
                    }
                }
            } else {
                pro.setProperty(key, jsonObject.getString(key));
            }
        }
        System.out.println(pro);
        pro.store(new FileOutputStream("src/main/resources/property.properties"),"Update  value");
        System.out.println(pro.getProperty("2[1].xianWinDrow"));
    }
}
